
import { ImagePreview } from 'vant';
import router from '../router/router.js'//路由模块
import layer from '../common/layer.js'//路由模块
import config from '@/config.js'//配置
import ajax from '../common/axios-td.js'//请求模块

const focus = {
    inserted: function (el) {
        el.focus()
    },
}
//v-go 跳转连接指令
const go={
    inserted: function (el,bindings) {
        var _val=bindings.value || '';
        el.addEventListener('click',()=>{
            if(_val.length<=0) {
                return;
            }
            window.location.hash=_val;
        })
    },
    unbind:(el)=>{
        //移除事件
        el.removeEventListener('click',el.click,false)
    }
}
const tgo={
    inserted: function (el,bindings) {
        var _val=bindings.value || '';
        el.addEventListener('click',()=>{
            if(_val.length<=0) {
                return;
            }
            var id= el.getAttribute('id') || '';
            if(id.length==32) {
                 //跳转前 请求接口获取是否可以购买票
                ajax.post({
                    url:'/api/webapi/Ticket/CheckTicketCanBuy',
                    data:{ticketId:id},
                    msg:true,
                    success:()=>{
                        window.location.hash=_val;
                    }
                })
                return;
            }
           
            window.location.hash=_val;
        })
    },
    unbind:(el)=>{
        //移除事件
        el.removeEventListener('click',el.click,false)
    }
}
const tel={
    inserted: function (el,bindings) {
        var _val=bindings.value || '';
        el.addEventListener('click',()=>{
            if(_val.length<=0) {
                return;
            }
            window.location.href='tel://'+_val;
        })
    },
    unbind:(el)=>{
        //移除事件
        el.removeEventListener('click',el.click,false)
    }
}
const rgo={
    inserted: function (el,bindings) {
        var _val=bindings.value || '';
        el.addEventListener('click',()=>{
            if(_val.length<=0) {
                return;
            }
            
            router.replace(_val)
        })
    },
    unbind:(el)=>{
        //移除事件
        el.removeEventListener('click',el.click,false)
    }
}
const seeimg={
    inserted:(el,bindings) =>{
        var _val=bindings.value || {};
        if(typeof _val!='object') {
            _val=JSON.parse(_val)
        }
        var timeout=_val.timeout || 0
        var tag=el.tagName.toUpperCase();
        if(tag!="IMG") {
            setTimeout(() => {
                var imgs=el.getElementsByTagName('img');
                if(imgs.length<=0) {
                    return;
                }
                var arr=[];//储存路径数组
                for(var i=0;i<imgs.length;i++) {
                    arr.push(imgs[i].getAttribute('big') || imgs[i].src);
                }
                for(var i=0;i<imgs.length;i++) {
                    imgs[i].addEventListener('click',(_e)=>{
                        var src=_e.target.src || '';
                        var target =_e.target;
                        var parent=target.parentElement; 
                        var liIndex = Array.prototype.indexOf.call(arr, src) || 0;
                        ImagePreview({
                            images: arr,
                            startPosition: liIndex,
                            loop:false,
                        });
                    })
                }
            }, timeout);
        } else {
            el.addEventListener('click',()=>{
                ImagePreview({
                    images: [(el.getAttribute('big') || el.src)],
                    startPosition: 0,
                    loop:false,

                });
            })
        }
    },
    unbind:(el)=>{
        var tag=el.tagName.toUpperCase();
        if(tag!="IMG") {
            var imgs=el.getElementsByTagName('img');
            if(imgs.length<=0) {
                return;
            }
            for(var i=0;i<imgs.length;i++) {
                imgs[i].removeEventListener('click',el.click,false)
            }
        } else {
            el.removeEventListener('click',el.click,false)
        }
    }
}
function checkWxScroll() {
    var currentPosition, timer;
    var speed = 1; 
    timer = setInterval(function() {
      currentPosition=document.documentElement.scrollTop || document.body.scrollTop;
      currentPosition-=speed;
      window.scrollTo(0,currentPosition);
      currentPosition+=speed; 
      window.scrollTo(0,currentPosition);
      clearInterval(timer);
    }, 1);
}
const iosbugscroll={
    inserted: function(el, binding, vnode) {
        var ua = navigator.userAgent.toLowerCase();
        
        if(/micromessenger/.test(ua)) {
          if(/iphone|ipad|ipod/.test(ua)) {
            var tag=el.tagName.toUpperCase();
            // layer.msg('tag='+tag)
            if(tag=='INPUT') {
                el.onblur = function temporaryRepair() {
                    // layer.msg('离开了='+tag)
                    setTimeout(function() {
                      checkWxScroll();
                    }, 200);
                  };
                return;
            }

            // input、textarea被组件包装的场景
            const childInput = el.getElementsByTagName('input');
            const childTextarea = el.getElementsByTagName('textarea');
            for (let i = 0; i < childInput.length; i++) { 
              childInput[i].onblur = function temporaryRepair() {
                setTimeout(function() {
                  checkWxScroll();
                }, 200);
              };
            }
           
            for (let i = 0; i < childTextarea.length; i++) {
                childTextarea[i].onblur = function temporaryRepair() {
                setTimeout(function() {
                  checkWxScroll();
                }, 200);
              };
            }
            // input、textarea中的场景
          }
        }
      }
}
const advert={
    inserted: function (el,bindings) {
        var obj=bindings.value || null;
        if(obj==null) {
            return;
        }
        //先获取 类型
        
        var type=obj.LINK_URL_TYPE;
        if(type==2) {
            return;
        }
        if(obj.LINK_URL.length<=0) {
            return;
        }
        

        el.addEventListener('click',()=>{
            var url='';
            if(obj.LINK_URL.indexOf('http')>=0) {
                url=obj.LINK_URL;
            } else {
                url=config.url[config.url.mode].domain+obj.LINK_URL;
            }
            window.location.href=url;
        })
    },
    unbind:(el)=>{
        //移除事件
        el.removeEventListener('click',el.click,false)
    }
}
export { focus,go,seeimg,rgo,tel,iosbugscroll,tgo,advert}